package com.kmxd.ams.core.util;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.kmxd.ams.core.common.constant.Constants;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 请求参数传递辅助类
 *
 * @author ZuoLG
 */
public class DynamicTableNameUtil {
  /** table */
  private static final ThreadLocal<String> TABLE_NAME = new ThreadLocal<>();

  DynamicTableNameUtil() {
    System.out.println("TABLE_NAME:" + TABLE_NAME);
  }

  /**
   * 设置请求参数
   *
   * @param tableName 请求参数 MAP 对象
   */
  public static void setTableName(String tableName) {
    TABLE_NAME.remove();
    TABLE_NAME.set(tableName);
  }

  /**
   * 获取请求参数
   *
   * @return 请求参数 MAP 对象
   */
  public static String getTableName() {
    return TABLE_NAME.get();
  }

  /**
   * 校验表名是否是文件，案卷，盒，项目
   *
   * @param tableName
   * @return
   */
  public static boolean checkTableName(String tableName) {
    String patternStr =
        "(f|F)[0-9]\\d*_[0-9]\\d*_(document|box|project|volume|DOCUMENT|BOX|PROJECT|VOLUME)";
    return checkTableNameFun(tableName, patternStr);
  }

  /**
   * 判断是否是文件表名
   *
   * @param tableName
   * @return
   */
  public static boolean checkTableDoc(String tableName) {
    String patternStr = "(f|F)[0-9]\\d*_[0-9]\\d*_(document|DOCUMENT)";
    return checkTableNameFun(tableName, patternStr);
  }

  /**
   * 判断是否是盒
   *
   * @param tableName
   * @return
   */
  public static boolean checkTableBox(String tableName) {
    String patternStr = "(f|F)[0-9]\\d*_[0-9]\\d*_(box|BOX)";
    return checkTableNameFun(tableName, patternStr);
  }

  /**
   * 判断是否是案卷
   *
   * @param tableName
   * @return
   */
  public static boolean checkTableVol(String tableName) {
    String patternStr = "(f|F)[0-9]\\d*_[0-9]\\d*_(volume|VOLUME)";
    return checkTableNameFun(tableName, patternStr);
  }

  /**
   * 判断是否是项目
   *
   * @param tableName
   * @return
   */
  public static boolean checkTableProj(String tableName) {
    String patternStr = "(f|F)[0-9]\\d*_[0-9]\\d*_(project|PROJECT)";
    return checkTableNameFun(tableName, patternStr);
  }

  /**
   * 判断是否是电子文件表
   * @param tableName
   * @return
   */
  public static boolean checkTableEfile(String tableName) {
    if (StrUtil.equals(tableName, Constants.TABLE_NAME_EFILE)) {
      return false;
    }
    String patternStr = "(i_efile_record_|I_EFILE_RECORD_)\\d*";
    return checkTableNameFun(tableName, patternStr);
  }

  /**
   * 校验表名方法
   *
   * @param tableName
   * @param patternStr
   * @return
   */
  public static boolean checkTableNameFun(String tableName, String patternStr) {
    if (ObjectUtil.isEmpty(tableName)) {
      return false;
    }
    Pattern pattern = Pattern.compile(patternStr);
    Matcher matcher = pattern.matcher(tableName);
    return matcher.matches();
  }

  public static void main(String[] args) {
    System.out.println(checkTableName("f0_0_document"));
    System.out.println(checkTableName("F1_1_box"));
    System.out.println(checkTableName("f1_1_document"));
    System.out.println(checkTableName("f1_2_volume"));
    System.out.println(checkTableName("f1_2_project"));
    System.out.println(checkTableName("f1_2_PROJECT"));
    System.out.println(checkTableName("t_transfer_document"));
    System.out.println(checkTableName("conf_i18n"));
    System.out.println(checkTableDoc("f12_121_document"));
    System.out.println(checkTableEfile("i_efile_record_12"));
  }
}
